package com.lancoo.znbkxx.teacher.ui.fragment

import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.appcompat.app.AppCompatDialogFragment
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager
import com.google.android.material.tabs.TabLayout
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.base.BaseVMFragment
import com.lancoo.znbkxx.teacher.model.data.ReviewedDetail
import com.lancoo.znbkxx.teacher.model.data.StuProgressInfo
import com.lancoo.znbkxx.teacher.utils.DensityUtils
import java.io.Serializable

/**
 * @author : Admin
 * @date   : 2019/11/28 19:39
 * @desc   :
 */
class ReviewedDialog : AppCompatDialogFragment() {
    lateinit var tabLayout: TabLayout
    private val fmtList by lazy { mutableListOf<BaseVMFragment<*, *>>() }

    private fun initViewPager(
            info: ReviewedDetail?,
            submittedList: MutableList<StuProgressInfo>?,
            tbl: TabLayout,
            vp: ViewPager
    ) {
        val fmtReviewed = ReviewedFragment.newInstance(
            info?.ReviewedList,
            submittedList?.filter { it.State == 2 || it.State == 3 } as? MutableList<StuProgressInfo>)
        val fmtUnReviewed = ReviewedFragment.newInstance(
            info?.NotReviewedList,
            submittedList?.filter { it.State == 0 || it.State == 1 } as? MutableList<StuProgressInfo>)
        fmtList.clear()
        fmtList.add(fmtReviewed)
        fmtList.add(fmtUnReviewed)
        tbl.addOnTabSelectedListener(TabLayout.ViewPagerOnTabSelectedListener(vp))
        vp.addOnPageChangeListener(TabLayout.TabLayoutOnPageChangeListener(tbl))
        vp.adapter = object :
            FragmentPagerAdapter(childFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            override fun getItem(position: Int): Fragment {
                return fmtList[position]
            }

            override fun getCount(): Int {
                return fmtList.size
            }
        }
    }

    companion object {
        private const val REVIEWED = "reviewed"
        private const val SUBMITTED = "submitted"
        fun newInstance(info: ReviewedDetail?, submittedList: MutableList<StuProgressInfo>?): ReviewedDialog {
            val fragment = ReviewedDialog()
            val bundle = Bundle()
            bundle.putParcelable(REVIEWED, info)
            bundle.putSerializable(SUBMITTED, submittedList as? Serializable)
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        return inflater.inflate(R.layout.dialog_reviewed, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        val info = arguments?.getParcelable<ReviewedDetail>("reviewed")
        val submittedList = arguments?.getSerializable("submitted")
        tabLayout = view.findViewById(R.id.tabLayout)
        val tab = tabLayout.newTab()
        tab.text = if (null != info) "已巩固学生" else "已提交学生"
        val tab2 = tabLayout.newTab()
        tab2.text = if (null != info) "未巩固学生" else "未提交学生"
        tabLayout.addTab(tab)
        tabLayout.addTab(tab2)
        val viewpager = view.findViewById<ViewPager>(R.id.vp)
        initViewPager(info, submittedList as? MutableList<StuProgressInfo>, tabLayout, viewpager)
        super.onViewCreated(view, savedInstanceState)
    }

    override fun onResume() {
        super.onResume()
        val param = this.dialog?.window?.attributes
        param?.width = DensityUtils.dp2px(context, 300f)
        param?.height = WindowManager.LayoutParams.WRAP_CONTENT
        dialog?.window?.attributes = param
    }
}